package cn.com.w.commons.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class ShutdownContext implements ApplicationContextAware {

    Logger logger = LoggerFactory.getLogger(ShutdownContext.class);

    private ConfigurableApplicationContext context;

    public void shutdown() {
        if (Objects.nonNull(context)) {
            logger.info("###########开始关闭系统############");
            context.close();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof ConfigurableApplicationContext) {
            this.context = (ConfigurableApplicationContext) applicationContext;
        }
    }
}